package com.example.ac;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    Button btn;
    EditText height, width;
    RadioButton boy, girl;
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.bth);
        height = findViewById(R.id.height);
        width = findViewById(R.id.weight);
        boy = findViewById(R.id.boy);
        girl = findViewById(R.id.girl);
        //设置监听
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //计算公式为:BMI = 体重 /身高2
                //体重单位:千克  身高单位:米
                String h = height.getText().toString();
                String w = width.getText().toString();
                double res = 0, heightNum = 0, weightNum = 0;
                String msg = "", bmi = "";
                //将字符串类型转换为浮点类型
                heightNum = Double.parseDouble(h);
                weightNum = Double.parseDouble(w);
                res = 10000 * weightNum / (heightNum * heightNum);//res为计算出的bmi值
                if (boy.isChecked()) {
                    if (res > 35) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi1).toString();
                    } else if (res > 30) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi2).toString();
                    } else if (res > 25) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi3).toString();
                    } else if (res > 20) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi4).toString();
                    } else {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi5).toString();
                    }
                }
                if (girl.isChecked()) {
                    if (res > 34) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi1).toString();
                    } else if (res > 29) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi2).toString();
                    } else if (res > 24) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi3).toString();
                    } else if (res > 19) {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi4).toString();
                    } else {
                        msg += "你的BMI值是" + String.format("%.2f", res) + "\n" + getText(R.string.levelbmi5).toString();
                    }
                }
//                Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
                //创建一个bundle类，存储数据
                Bundle bundle = new Bundle();
                bundle.putString("message", msg);

                //执行跳转
                Intent i = new Intent(MainActivity.this, ResultActivity.class);
                i.putExtras(bundle);
                startActivity(i);
            }
        });
    }

    //创建选项菜单(系统菜单)
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        //添加菜单项
        menu.add(1, 1, 1, "版本");
        menu.add(1, 2, 2, "使用说明");
        return true;
    }
    //菜单项事件

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(getApplicationContext(), "版本1.0, 400杨楹贝制作", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                onClick();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    public void onClick() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("使用说明");
        dialog.setMessage("输入身高、体重、选择性别,点击按钮便可查看结果");
        dialog.setPositiveButton("确定", new okClick());
        dialog.create();
        dialog.show();

    }

    private class okClick implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    }
}